package com.iuie.ns.web.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iuie.ns.web.beans.DataFieldVO;
import com.iuie.ns.web.html.HTMLTag;

/**
 * @desc 数据表格的数据集标签定义
 * @createTime 2017年7月6日 下午3:34:49
 * @author iuie
 */
public class DataSetTag extends SimpleTagSupport{
    
    private String id;
    
    private String idField;
    
    private List<DataFieldVO> dataFields;
    
    @Override
    public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
        
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> dataset = new HashMap<String, Object>();
        dataset.put("id", id);
        dataset.put("idField", idField);
        dataset.put("columns", dataFields);
        String dataSet = mapper.writeValueAsString(dataset);
        JspWriter writter = getJspContext().getOut();
        String js = "var " + id + " = " + dataSet;
        
        HTMLTag tag = new HTMLTag("script");
        tag.addAttribute("type", "text/javascript");
        tag.setTextNode(js);
        
        writter.write(tag.toString());
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getIdField() {
        return idField;
    }
    public void setIdField(String idField) {
        this.idField = idField;
    }
    public List<DataFieldVO> getDataFields() {
        if (this.dataFields == null ) {
            this.dataFields = new ArrayList<>(3);
        }
        return this.dataFields;
    }
}
